package Compation;

import java.util.*;

/**
 * 示例 1：
 *
 * 输入：words = ["mass","as","hero","superhero"]
 * 输出：["as","hero"]
 * 解释："as" 是 "mass" 的子字符串，"hero" 是 "superhero" 的子字符串。
 * ["hero","as"] 也是有效的答案。
 * 示例 2：
 *
 * 输入：words = ["leetcode","et","code"]
 * 输出：["et","code"]
 * 解释："et" 和 "code" 都是 "leetcode" 的子字符串。
 * 示例 3：
 *
 * 输入：words = ["blue","green","bu"]
 * 输出：[]
 */
public class stringMatching {
    public static void main(String[] args) {
        String[] words = new String[]{"mass","as","hero","superhero"};
        System.out.println(new stringMatching().stringMatching(words));
    }

    public List<String> stringMatching(String[] words) {
        if (words == null || words.length == 0) {
            return new ArrayList<>();
        }
        Set<String> result = new HashSet<>();
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < words.length; j++) {
                if (words[j].contains(words[i]) && j != i) {
                    result.add(words[i]);
                    continue;
                }
            }
        }
        return new ArrayList<>(result);
    }
}
